﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Orderers;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Warehousing;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Warehousings
{
    public class OrderTineUpdateHandler : IRequestHandler<OrderTineUpdateCommend, Result>
    {
        private readonly IBaseRepository<OrderTine> OrderTine;
        private readonly IMapper mapper;

        public OrderTineUpdateHandler(IBaseRepository<OrderTine> OrderTine, IMapper mapper)
        {
            this.OrderTine = OrderTine;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(OrderTineUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<OrderTine>(request);
            string sql = "UPDATE OrderTine SET InventoryInfoId=@InventoryInfoId ,Prices=@Prices ,Nums=@Nums ,Unit=@Unit " +
                         ",Summary=@Summary WHERE OrderTineId =@OrderTineId";
            var list = OrderTine.Update(sql, new
            {
                OrderTineId = AddData.OrderTineId,
                InventoryInfoId = AddData.InventoryInfoId,
                Prices = AddData.Prices,
                Nums = AddData.Nums,
                Unit = AddData.Unit,
                Summary = AddData.Summary,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
